package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 分页查询
     */
    @Override
    public PageResult<Course> list(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        //1.设置PageHelper分页参数
        PageHelper.startPage(page, pageSize);
        //2.调用mapper接口方法，执行查询
        List<Course> deptList = courseMapper.selectList(name, subject, target);
        //3.解析并封装分页结果
        Page<Course> p = (Page<Course>) deptList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }
    /*
    * 添加课程
    * */
    @Override
    public void add(Course course) {
        //补全数据
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        //调用mapper
        courseMapper.insert(course);
    }

    /*
    * 根据id删除课程
    * */
    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    /*
    * 根据id修改课程
    * */
    @Override
    public void update(Course course) {
        //补全数据
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    /*
    * 根据id查询课程
    * */
    @Override
    public Course selectById(Integer id) {
      return   courseMapper.selectById(id);
    }

    /*
    * 查询所有课程
    * */
    @Override
    public List<Course> selectAll() {
        return courseMapper.selectAll();
    }

    /*
    * 根据课程类别查询课程
    * */
    @Override
    public List<Course> selectBySubject(Integer subject) {
        return courseMapper.selectBySubject(subject);
    }
}
